

import java.lang.StringBuffer;
import java.util.Iterator;
import java.util.Collection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * This class is an aggregation of handy function that ease development.
 */
public class Util {
 
	/** This flag indicates whether cache--for the sentence entry--should be enabled or not. */
	public final static boolean globalFlagUseCache = true;
	
	/** The Constant RelativeDataDir. */
	public final static String RelativeDataDir = "data";

    /**
     * Gets the data path of filepath.
     * Uses RelativeDataDir as the parent path.
     * 
     * @param filepath the filepath
     * 
     * @return the data path of filepath.
     */
    public static String getDataPathOf(String filepath) {
        return (new File(RelativeDataDir, filepath)).getPath();
    }
    	
    /**
     * Join given collection using delimiter.
     * 
     * @param s collection of Strings.
     * @param delimiter the delimiter that will be inserted between each string.
     * 
     * @return a joined string.
     */
    public static String join(Collection<String> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<String> iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
    
    /**
     * Load object using {@link Serializable} interface.
     * 
     * @param fileName the file name
     * 
     * @return the object
     */
    public static Object loadObject(String fileName){
		FileInputStream fileIn = null;
		ObjectInputStream objectIn = null;
		
		Object o = null;
		
		fileName = "data/cache/" + fileName;
		
		try{
			fileIn = new FileInputStream(fileName);
			objectIn = new ObjectInputStream(fileIn);
			
			o = objectIn.readObject();
			
			objectIn.close();
			fileIn.close();
		}catch(FileNotFoundException e){
		}catch(Exception e){	
		}
		
		return o;
	}
	
    /**
     * Save object using {@link Serializable} interface.
     * 
     * @param fileName the file name
     * @param o the o
     */
    public static void saveObject(String fileName, Object o){
		FileOutputStream fileOut = null;
		ObjectOutputStream objectOut = null;
		
		fileName = "data/cache/" + fileName;
		
		try{
			fileOut = new FileOutputStream(fileName);
			objectOut = new ObjectOutputStream(fileOut);
			
			objectOut.writeObject(o);
			
			objectOut.flush();
			objectOut.close();
			fileOut.close();
		}catch(Exception e){
		}
	}
    
	/**
	 * Checks if a given word parameter is relevant. 
	 * (whether it is adjective, adverb, verb, or model verb)
	 * 
	 * @param word the word
	 * 
	 * @return true, if is relevant tag
	 */
	public static boolean isRelevantTag(TaggedWordExt word){
		//return true;
		return word.isNoun();
		//return word.isNoun() || word.isVerb() || word.isAdjective() || word.isAdverb();
		//return word.isAdjective() || word.isAdverb() || word.isVerb() || word.isModal();
	}
}